Conversion between c sharp Stream and byte[]
- 2020-05-05 11:48:17
- OfStack
/* - - - - - - - - - - - - - - - - - - - - - - - -
* Stream and byte[] conversion
* - - - - - - - - - - - - - - - - - - - - - - - */
/// < summary >
/// converts Stream to byte[]
/// < /summary >
public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// sets the position of the current stream to
at the beginning of the stream stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
/// < summary >
/// converts byte[] to Stream
/// < /summary >
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
/* - - - - - - - - - - - - - - - - - - - - - - - -
* Stream and files * - - - - - - - - - - - - - - - - - - - - - - - */
/// < summary >
/// writes Stream to
/// < /summary >
public void StreamToFile(Stream stream,string fileName)
{
// convert Stream to byte[]
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// sets the position of the current stream to the beginning of the stream
stream.Seek(0, SeekOrigin.Begin);
// write byte[] to
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bytes);
bw.Close();
fs.Close();
}
/// < summary >
/// reads Stream
from the file /// < /summary >
public Stream FileToStream(string fileName)
{
// open
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
// reads byte[]
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Close();
// convert byte[] to Stream
Stream stream = new MemoryStream(bytes);
return stream;
}
* Stream and byte[] conversion
* - - - - - - - - - - - - - - - - - - - - - - - */
/// < summary >
/// converts Stream to byte[]
/// < /summary >
public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// sets the position of the current stream to
at the beginning of the stream stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
/// < summary >
/// converts byte[] to Stream
/// < /summary >
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
/* - - - - - - - - - - - - - - - - - - - - - - - -
* Stream and files * - - - - - - - - - - - - - - - - - - - - - - - */
/// < summary >
/// writes Stream to
/// < /summary >
public void StreamToFile(Stream stream,string fileName)
{
// convert Stream to byte[]
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// sets the position of the current stream to the beginning of the stream
stream.Seek(0, SeekOrigin.Begin);
// write byte[] to
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bytes);
bw.Close();
fs.Close();
}
/// < summary >
/// reads Stream
from the file /// < /summary >
public Stream FileToStream(string fileName)
{
// open
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
// reads byte[]
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Close();
// convert byte[] to Stream
Stream stream = new MemoryStream(bytes);
return stream;
}